Imports DAL
Imports MyClasses
Imports System.Collections
Imports System.Configuration

Public Class frmLocation
    Dim _intLocNo As Integer
    Public _intLocID As Integer
    Public flgLocation As Byte

    Private Sub SetLocationName()
        Try
            txtLocName.Text = Trim(txtShelfNo.Text) & IIf(Trim(txtRowNo.Text) <> "", "-" & txtRowNo.Text, "") & IIf(Trim(txtColNo.Text) <> "", "-" & txtColNo.Text, "")

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "checkLocationName")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub displayData()
        Try
            Call clearData()

            BCommon.SetTextReadOnly(txtLocName) 'Divya 31/01/2008 : Added for ReadOnly fields
            BCommon.SetComboValues(cmbLocType, "LOCTYPE", False)
            Call SetcontrolTag()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub saveData()
        Dim Location As CLocation
        Dim intResult As Integer
        Try
            Location = SetData()
            If Not IsNothing(Location) Then
                intResult = BLocation.save(Location, IIf(btnSave.Text = "&Save", "S", "U"))
                Select Case intResult
                    Case 0
                        BCommon.ShowMsgError(4)
                    Case -1
                        'Location Code Already Exist
                        BCommon.ShowMsgError(10053)
                    Case Else
                        BCommon.ShowMsgError(3)
                        btnSave.Text = "&Save"
                        Call clearData()
                        'Modified By:-VINIT(2008/01/18)
                        Call SetcontrolTag()
                        _intLocID = BLocation._intLocID
                End Select
            End If
            cmbLocType.Focus()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub clearData()
        Try
            txtColNo.Text = ""
            txtColNo.Tag = ""

            txtDescp.Text = ""
            txtDescp.Tag = ""

            txtLocName.Text = ""
            txtLocName.Tag = ""

            txtRowNo.Text = ""
            txtRowNo.Tag = ""

            txtShelfNo.Text = ""
            txtShelfNo.Tag = ""
            Me.Text = Me.Tag
            btnSave.Text = "&Save"

            If flgLocation = 1 Then btnSearch.Enabled = False Else btnSearch.Enabled = True

            btnDelete.Enabled = False
            If cmbLocType.Items.Count > 0 Then cmbLocType.SelectedIndex = 0 : cmbLocType.Tag = 0

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "clearData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function ValidateData() As Boolean
        Try
            If Trim(cmbLocType.Text) = "" Then
                BCommon.ShowMsgError(10051)
                cmbLocType.Focus()
                Me.Cursor = Cursors.Default
                Return False
                Exit Function
            End If
            If Trim(txtShelfNo.Text) = "" Then
                BCommon.ShowMsgError(10052)
                txtShelfNo.Focus()
                Me.Cursor = Cursors.Default
                Return False
                Exit Function
            End If
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Function SetData() As CLocation
        Dim objLoc As New CLocation
        Try
            With objLoc
                .LocID = _intLocNo
                .LocName = Trim(txtLocName.Text)
                .LocType = Trim(cmbLocType.SelectedValue)
                .LocShelf = Trim(txtShelfNo.Text)
                .LocRow = IIf(Trim(txtRowNo.Text) <> "", Trim(txtRowNo.Text), 0)
                .LocColn = IIf(Trim(txtColNo.Text) <> "", Trim(txtColNo.Text), 0)
                .LocDescrp = Trim(txtDescp.Text)
            End With
            Return objLoc
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try
    End Function

    Private Sub frmLocation_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If IsUpdated() Then
                    Select Case BCommon.ShowMsgError(10081, "YNC")
                        Case Windows.Forms.DialogResult.Yes
                            'Modified By:-Vinit(20071229)
                            If ValidateData() = True Then
                                Call saveData()
                            Else
                                e.Cancel = True
                            End If
                        Case Windows.Forms.DialogResult.Cancel
                            e.Cancel = True
                        Case Windows.Forms.DialogResult.No
                    End Select
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "button1_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmLocation_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Location")
        e.Cancel = True
    End Sub

    Private Sub frmLocation_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Location")
        End If
    End Sub

    Private Sub frmLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Call BCommon.SetFormStandards(Me)
            Call displayData()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmLocation_Load")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Call clearData()
            Call SetcontrolTag()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If ValidateData() = True Then Call saveData()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbLocType_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbLocType.KeyDown
        'Rahul_ITS@2007113 : Add following line
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            txtShelfNo.Focus()
        End If
    End Sub

    Private Sub txtShelfNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtShelfNo.KeyDown
        Try
            ' Divya@29/01/2008
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then txtRowNo.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True : Me.Cursor = Cursors.Default : Exit Sub
                End If
            End If
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
            '>>End
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtShelfNo_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtRowNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRowNo.KeyDown
        Try
            'Rahul_ITS@20071113 : Add following line
            If e.KeyCode = Keys.Enter Then txtColNo.Focus()
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.Shift Then e.SuppressKeyPress = True : Me.Cursor = Cursors.Default : Exit Sub
            If BCommon.ValidateText(e.KeyCode, "NUM", False) = False Then    'Divya 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRowNo_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtDescp_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescp.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            btnSave.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If

    End Sub

    Private Sub txtColNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtColNo.KeyDown
        Try
            'Rahul_ITS@20071113 : Add following line
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then txtDescp.Focus()
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.Shift Then e.SuppressKeyPress = True : Me.Cursor = Cursors.Default : Exit Sub
            If BCommon.ValidateText(e.KeyCode, "NUM", False) = False Then    'Divya 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtColNo_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim form As New frmMasterBrowse
            form.strSearch = "Location"
            'Rahul_ITS@20072612
            '<<START
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            '>>END
            form.ShowDialog()
            _intLocNo = form.strRecordNo
            Call clearData()
            If _intLocNo <> 0 Then
                Call ViewData()
                btnSave.Text = "&Update"
                'Modified By:-Vinit(2008/03/27)
                'Enabled the Delete Button on the basis of Financial Year
                If ConfigurationManager.AppSettings("FormMode") = "R" Then
                    btnDelete.Enabled = False
                Else
                    btnDelete.Enabled = True
                End If
                Me.ToolTipNormal.SetToolTip(btnSave, "Update")
            End If
            'Modified By:-VINIT(2008/01/18)
            Call SetcontrolTag()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub ViewData()
        Dim dtsLoc As New DataSet
        Try
            dtsLoc = BLocation.Search(_intLocNo)
            With dtsLoc.Tables(0).Rows(0)
                txtLocName.Text = .Item(0)
                txtLocName.Tag = .Item(0)
                'Me.Text = Me.Tag + " " + "(" + Trim(txtLocName.Text) + ")"

                cmbLocType.SelectedValue = .Item(1)
                cmbLocType.Tag = .Item(1)

                txtShelfNo.Text = .Item(2)
                txtShelfNo.Tag = .Item(2)

                'Divya_sinha@20071018
                txtRowNo.Text = .Item(3)
                txtRowNo.Tag = .Item(3)

                If (txtRowNo.Text = 0) Then
                    txtRowNo.Text = ""
                End If

                txtColNo.Text = .Item(4)
                txtColNo.Tag = .Item(4)

                If (txtColNo.Text = 0) Then
                    txtColNo.Text = ""
                End If
                '>>End Divya

                txtDescp.Text = .Item(5)
                txtDescp.Tag = .Item(5)
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "viewdata")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtColNo_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtColNo.LostFocus
        Try
            Call SetLocationName()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtColNo_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtShelfNo_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtShelfNo.LostFocus
        Try
            Call SetLocationName()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtShelfNo_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtRowNo_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRowNo.LostFocus
        Try
            Call SetLocationName()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRowNo_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function IsUpdated() As Boolean
        If Trim(txtLocName.Text) <> Trim(txtLocName.Tag) Or Trim(txtShelfNo.Text) <> Trim(txtShelfNo.Tag) Or _
        Trim(txtRowNo.Text) <> Trim(txtRowNo.Tag) Or Trim(txtColNo.Text) <> Trim(txtColNo.Tag) _
        Or Trim(txtDescp.Text) <> Trim(txtDescp.Tag) Or Trim(cmbLocType.Text) <> Trim(cmbLocType.Tag) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub txtLocName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLocName.KeyDown
        Try
            'Rahul_ITS@20071113 : Add following line
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then txtLocName.Focus()
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.Shift Then e.SuppressKeyPress = True : Me.Cursor = Cursors.Default : Exit Sub
            If BCommon.ValidateText(e.KeyCode, "NUM", False) = False Then    'Divya 23/10/2007
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtLocName_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    '**********************************************
    'Modified BY:-VINIT(2008/01/18)
    'Set the tag of the control for data validation
    '==============================
    Private Sub SetcontrolTag()
        cmbLocType.Tag = cmbLocType.Text
        txtColNo.Tag = txtColNo.Text
        txtDescp.Tag = txtDescp.Text
        txtLocName.Tag = txtLocName.Text
        txtRowNo.Tag = txtRowNo.Text
        txtShelfNo.Tag = txtShelfNo.Text
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If (BLocation.IsLocationUsed(_intLocNo)) = 0 Then
                If (BLocation.DeleteLocation(_intLocNo)) > 0 Then
                    BCommon.ShowMsgError(10372)
                    txtLocName.Focus()
                    displayData()
                    btnSave.Text = "&Save"
                    btnDelete.Enabled = False
                End If
            Else
                BCommon.ShowMsgError(10370)
                btnDelete.Focus()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnDelete_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
End Class
